//
//  ViewController.m
//  13-gcd队列组
//
//  Created by J on 16/2/25.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)UIImage* image1;
@property(nonatomic,strong)UIImage* image2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

void downLoad(void* data){
    NSLog(@"%@",[NSThread currentThread]);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
// dispatch_queue_t queue =   dispatch_get_global_queue(0, 0);
//    dispatch_async_f(queue, NULL, downLoad);//传一个函数指针
//
    
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    dispatch_group_async(group, queue, ^{

        NSURL* ur = [NSURL URLWithString:@"http://img1.imgtn.bdimg.com/it/u=2623466571,2046032480&fm=206&gp=0.jpg"];
        NSData* data = [NSData dataWithContentsOfURL:ur];
        UIImage* image = [UIImage imageWithData:data];
        self.image1 = image;
        
        NSLog(@"1%@",image);
    });
    
    dispatch_group_async(group, queue, ^{
        NSURL* ur = [NSURL URLWithString:@"http://v1.qzone.cc/avatar/201404/20/11/02/535338d28800c937.jpg%21200x200.jpg"];
        NSData* data = [NSData dataWithContentsOfURL:ur];
        UIImage* image = [UIImage imageWithData:data];
        self.image2 = image;
        
        NSLog(@"2%@",image);
    });
    
  
    dispatch_group_notify(group, queue, ^{
        
        UIGraphicsBeginImageContext(CGSizeMake(100,100));
        
        [self.image1 drawInRect:CGRectMake(0, 0, 50, 100)];
        [self.image2 drawInRect:CGRectMake(50, 0, 50, 100)];
        UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image  = image;
        });

        
        UIGraphicsEndImageContext();
        
    });
}

@end
